package com.xintoucloud.pbs.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xintoucloud.pbs.dao.PartyMemberDao;
import com.xintoucloud.pbs.entity.PartyMember;
import com.xintoucloud.pbs.model.DataGrid;
import com.xintoucloud.pbs.model.PartyDto;
import com.xintoucloud.pbs.service.PartyMemberService;
@Service@Transactional
public class PartyMemberServiceImpl implements PartyMemberService{
    @Autowired
	private PartyMemberDao partyMemberDao;
	@Override
	public boolean save(PartyMember partyMember) {
		return partyMemberDao.save(partyMember)!=null;
	}

	@Override
	public void delete(Integer id) {
		partyMemberDao.delete(id);
	}

	@Override
	@Transactional(readOnly=true)
	public PartyMember getOne(Integer id) {
		return partyMemberDao.getOne(id);
	}

	@Override
	@Transactional(readOnly=true)
	public List<PartyDto> list() {
		List<PartyMember> partyMembers = partyMemberDao.list();
		
		List<PartyDto> partyDtos = new ArrayList<>();
		for (PartyMember partyMember : partyMembers) {
			PartyDto partyDto = new PartyDto();
			partyDto.setId(partyMember.getPid());
			partyDto.setName(partyMember.getPname());
			partyDto.setPhone(partyMember.getPhone());
			partyDto.setSex(partyMember.getSex());
			partyDto.setEmail(partyMember.getEmail());
			partyDto.setRname(partyMember.getRname());
			partyDto.setIdnum(partyMember.getIdnum());
			partyDto.setState(partyMember.getPms().getState());
			partyDto.setOrg(partyMember.getOrg().getOname());
			partyDto.setPmp(partyMember.getPmp().getPermission());
			partyDtos.add(partyDto);
		}
		
		return partyDtos;
	}

	@Override@Transactional(readOnly=true)
	public PartyMember login(String pname, String ppswd) {
		PartyMember loginPartyMember = partyMemberDao.login(pname);
		System.out.println("密码"+loginPartyMember.getPpswd());
		if(loginPartyMember.getPpswd().equals(ppswd)) {
			return loginPartyMember;
		}
		return null;
	}

	@Override
	public void update(PartyMember partyMember) {
		partyMemberDao.update(partyMember);
	}

	@Override
	public void deleteById(int id) {
		 partyMemberDao.delete(id);
	}

	@Override
	public DataGrid<PartyDto> list(int pages, int rows) {
		DataGrid <PartyDto> data=new DataGrid<>();
		int pageIndex = (pages - 1) * rows;
		//查询总条数
		long count=partyMemberDao.count();
		//分页查询
		List<PartyMember> partyMembers=partyMemberDao.list(pageIndex,rows);
		List<PartyDto> partyDtos=new ArrayList<>();
		for (PartyMember partyMember : partyMembers) {
			PartyDto partyDto=new PartyDto();
			partyDto.setId(partyMember.getPid());
			partyDto.setName(partyMember.getPname());
			partyDto.setPhone(partyMember.getPhone());
			partyDto.setSex(partyMember.getSex());
			partyDto.setEmail(partyMember.getEmail());
			partyDto.setRname(partyMember.getRname());
			partyDto.setIdnum(partyMember.getIdnum());
			partyDto.setState(partyMember.getPms().getState());
			partyDto.setOrg(partyMember.getOrg().getOname());
			partyDto.setPmp(partyMember.getPmp().getPermission());
			partyDtos.add(partyDto);
		}
		data.setCount(count);
		data.setData(partyDtos);
		if(partyDtos!=null) {
			data.setCode(200);
			data.setMsg("加载成功");
		}
		return data;
	}

	@Override
	public DataGrid<PartyDto> list2(int pages, int rows) {
		DataGrid <PartyDto> data=new DataGrid<>();
		int pageIndex = (pages - 1) * rows;
		//查询总条数
		long count=partyMemberDao.count();
		//分页查询
		List<PartyMember> partyMembers=partyMemberDao.list2(pageIndex,rows);
		List<PartyDto> partyDtos=new ArrayList<>();
		for (PartyMember partyMember : partyMembers) {
			PartyDto partyDto=new PartyDto();
			partyDto.setId(partyMember.getPid());
			partyDto.setName(partyMember.getPname());
			partyDto.setPhone(partyMember.getPhone());
			partyDto.setSex(partyMember.getSex());
			partyDto.setEmail(partyMember.getEmail());
			partyDto.setRname(partyMember.getRname());
			partyDto.setIdnum(partyMember.getIdnum());
			partyDto.setState(partyMember.getPms().getState());
			partyDto.setOrg(partyMember.getOrg().getOname());
			partyDto.setPmp(partyMember.getPmp().getPermission());
			partyDtos.add(partyDto);
		}
		data.setCount(count);
		data.setData(partyDtos);
		if(partyDtos!=null) {
			data.setCode(200);
			data.setMsg("加载成功");
		}
		return data;
	}

	@Override
	public DataGrid<PartyDto> list3(int pages, int rows) {
		System.out.println("33333333333333333333333333");
		DataGrid <PartyDto> data=new DataGrid<>();
		int pageIndex = (pages - 1) * rows;
		//查询总条数
		long count=partyMemberDao.count();
		//分页查询
		List<PartyMember> partyMembers=partyMemberDao.list3(pageIndex,rows);
		List<PartyDto> partyDtos=new ArrayList<>();
		for (PartyMember partyMember : partyMembers) {
			PartyDto partyDto=new PartyDto();
			partyDto.setId(partyMember.getPid());
			partyDto.setName(partyMember.getPname());
			partyDto.setPhone(partyMember.getPhone());
			partyDto.setSex(partyMember.getSex());
			partyDto.setEmail(partyMember.getEmail());
			partyDto.setRname(partyMember.getRname());
			partyDto.setIdnum(partyMember.getIdnum());
			partyDto.setState(partyMember.getPms().getState());
			partyDto.setOrg(partyMember.getOrg().getOname());
			partyDto.setPmp(partyMember.getPmp().getPermission());
			partyDtos.add(partyDto);
		}
		data.setCount(count);
		data.setData(partyDtos);
		if(partyDtos!=null) {
			data.setCode(200);
			data.setMsg("加载成功");
		}
		return data;
	}

	@Override
	public DataGrid<PartyDto> list4(int oid,int pages, int rows) {
		DataGrid <PartyDto> data=new DataGrid<>();
		int pageIndex = (pages - 1) * rows;
		//查询总条数
		long count=partyMemberDao.count();
		//分页查询
		List<PartyMember> partyMembers=partyMemberDao.list4(oid,pageIndex,rows);
		List<PartyDto> partyDtos=new ArrayList<>();
		for (PartyMember partyMember : partyMembers) {
			PartyDto partyDto=new PartyDto();
			partyDto.setId(partyMember.getPid());
			partyDto.setName(partyMember.getPname());
			partyDto.setPhone(partyMember.getPhone());
			partyDto.setSex(partyMember.getSex());
			partyDto.setEmail(partyMember.getEmail());
			partyDto.setRname(partyMember.getRname());
			partyDto.setIdnum(partyMember.getIdnum());
			partyDto.setState(partyMember.getPms().getState());
			partyDto.setOrg(partyMember.getOrg().getOname());
			partyDto.setPmp(partyMember.getPmp().getPermission());
			partyDtos.add(partyDto);
		}
		data.setCount(count);
		data.setData(partyDtos);
		if(partyDtos!=null) {
			data.setCode(200);
			data.setMsg("加载成功");
		}
		return data;
	}

}
